package com.tianqu.tms.www.rental.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改-高级搜索 action
 * @author mufeiyu
 *
 */
public class RentalContractSearchInputAction extends TmsAction {

	
	private Logger logger = Logger.getLogger(RentalContractSearchInputAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		Codes codes = Codes.getInstance();
		
		//去掉缓存
		if(uc.getAttribute("seniorForm")!=null)
		{
			uc.removeAttribute("seniorForm");
		}
		
		RentalContractAddInputViewHelper viewHelper = new RentalContractAddInputViewHelper();

		String searchButton = request.getParameter("searchButton");
		
		// 初次
		if (StringUtil.isNullOrBlank(searchButton)) {
			// 初次
			viewHelper.setSerial("");
			viewHelper.setLocationSerial("");
			viewHelper.setOwnerName("");
			viewHelper.setRentalArea(0);
			viewHelper.setStartDateS("");
			viewHelper.setStartDateE("");
			viewHelper.setEndDateS("");
			viewHelper.setEndDateE("");
			
			// 合同状态
			List contractStatusList = codes.getCodeslistByItemId(Codes.CONTRACT_STATUS);

			for (Object object : contractStatusList) {
				String checked = "checked";
				HashMap codesMap = (HashMap)object;
				codesMap.put("checked", checked);
			}
			
			// 合同状态列表
			viewHelper.setContractStatusList(contractStatusList);
			
			// 租位区域列表
			List areaList = codes.getCodeslistByItemId(Codes.RENTAL_AREA);
			viewHelper.setRentalAreaList(areaList);
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			uc.setAttribute("searchFlg", "1");

		} else {
			
			// Controller验证失败
			if("true".equals(uc.getAttribute("errFlg"))){
				viewHelper = (RentalContractAddInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
				uc.removeAttribute(TmsConstants.VIEWHELP);
				uc.removeAttribute("errFlg");
				uc.setAttribute("searchFlg", "1");

			} else if (!StringUtil.isNullOrBlank(searchButton) && "1".equals(searchButton)){
				// validation-rental.xml验证失败				
				RentalContractAddInputForm rentalForm = (RentalContractAddInputForm) uc.getInputForm();
				
				if (null != rentalForm) {
					viewHelper.setSerial(rentalForm.getSerial());
					viewHelper.setLocationSerial(rentalForm.getLocationSerial());
					viewHelper.setOwnerName(rentalForm.getOwnerName());
					viewHelper.setRentalArea(rentalForm.getRentalArea());
					viewHelper.setStartDateS(rentalForm.getStartDateS());
					viewHelper.setStartDateE(rentalForm.getStartDateE());
					viewHelper.setEndDateS(rentalForm.getEndDateS());
					viewHelper.setEndDateE(rentalForm.getEndDateE());
					
					// 合同状态列表
					viewHelper.setContractStatusList(convertContractStatus(rentalForm.getContractStatus()));
					
					// 租位区域列表
					List areaList = codes.getCodeslistByItemId(Codes.RENTAL_AREA);
					viewHelper.setRentalAreaList(areaList);
				}
			}
		}
		request.setAttribute(TmsConstants.VIEWHELP, viewHelper);
		uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
		
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	private List convertContractStatus(String[] contractStatus) throws TmsDaoException{
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		if(null != contractStatus && contractStatus.length > 0){
			for (int i = 0; i < contractStatus.length; i++){
				int itemCode = StringUtils.isNumeric(contractStatus[i])?Integer.parseInt(contractStatus[i]):-1;
				map.put(itemCode, itemCode);
			}
		}
		List codeslist = Codes.getInstance().getCodeslistByItemId(Codes.CONTRACT_STATUS);
		for (Object object : codeslist) {
			String checked = "";
			HashMap codesMap = (HashMap)object;
			if(map.get(codesMap.get("itemCode"))!=null ) checked = "checked";
			codesMap.put("checked", checked);
		}
		return codeslist;
	}
}